import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import {fetchOrderList}from '../api/order'

export const useOrderBadgeStore = defineStore(
  'orderBadge',
  async() => {
    
	const backendList = ref([])
	const res = await fetchOrderList({
		orderStatus:'已下单',
	})
	backendList.value = res.data.records
	
    
    const unhandledIds = computed(() =>
      backendList.value.map(o => o.customerId)
    )
    const hasUnread = computed(() => unhandledIds.value.length > 0)

    function add(id) { if (!unhandledIds.value.includes(id)) unhandledIds.value.push(id) }
    function remove(id) { unhandledIds.value = unhandledIds.value.filter(i => i !== id) }

    return { unhandledIds, hasUnread, add, remove }
  },
  {
    persist: {
      // 小程序 Storage API
      storage: {
        setItem(key, value) { uni.setStorageSync(key, value) },
        getItem(key) { return uni.getStorageSync(key) }
      }
    }
  }
)